package com.example.mrjiangprogressbar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class Flower extends View{
    private int locationX;
    private int locationY;
    private boolean isAlive = true;
    private int frameIndex = 0;
    private int mapIndex;
    private long lastBirthTime = 0l;
    private Bitmap  mFlowerBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.flower);;
    private RectF FlowerBitmapRect;
    private RectF mFlowerBitmapRect= new RectF();

    public Flower(Context context) {
        super(context);
    }

    public Flower(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Flower(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public Flower(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isAlive) {
            frameIndex = (++frameIndex) % 8;
            if(FlowerBitmapRect==null){
                mFlowerBitmapRect.set(0.0f, 0.0f,  50.0f , 50.0f);
                canvas.drawBitmap(mFlowerBitmap,null,FlowerBitmapRect,null);
            }else {
                this.mFlowerBitmapRect.set(FlowerBitmapRect.left,FlowerBitmapRect.top+20+frameIndex*6,FlowerBitmapRect.right,FlowerBitmapRect.bottom);
                canvas.drawBitmap(mFlowerBitmap,null,mFlowerBitmapRect,null);
            }
        }
    }
    public void setFlowerBitmapRect(RectF flowerBitmapRect) {
        if (FlowerBitmapRect==null){
            FlowerBitmapRect= new RectF();
        }
        FlowerBitmapRect = flowerBitmapRect;
        invalidate();
    }
}
